﻿
Partial Class Apps_Shares_Uc_sub_menu_fv
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If Not IsPostBack Then
                InitCtrl()
            End If
        Catch ex As Exception
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try
    End Sub

    Sub InitCtrl()
        Dim BreadCrumpBG As HtmlControl = Page.Master.FindControl("tdBreadCrump")
        Dim BreadCrump As Literal = Page.Master.FindControl("ltrBreadCrump")
        'Change mode by querystring
        If Not String.IsNullOrEmpty(Request.QueryString("ReadOnly")) Then
            CodeMatrix.Business.GenericData.SetBreadCrump(BreadCrumpBG, BreadCrump, 29)
            fv.ChangeMode(FormViewMode.ReadOnly)
        ElseIf Not String.IsNullOrEmpty(Request.QueryString("Insert")) Then
            CodeMatrix.Business.GenericData.SetBreadCrump(BreadCrumpBG, BreadCrump, 30)
            fv.ChangeMode(FormViewMode.Insert)
        ElseIf Not String.IsNullOrEmpty(Request.QueryString("Edit")) Or String.IsNullOrEmpty(Request.QueryString("Update")) Then
            CodeMatrix.Business.GenericData.SetBreadCrump(BreadCrumpBG, BreadCrump, 31)
            fv.ChangeMode(FormViewMode.Edit)
        End If
    End Sub

    'Protected Sub fvSrc_Updated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles fvSrc.Updated
    '    Try
    '        'To list page
    '        If e.Exception Is Nothing Then
    '            Response.Redirect("~/apps/sub_menu_list.aspx?Edit=true")
    '        Else
    '            GenericHelper.ShowAlert(Me, e.Exception)
    '        End If
    '    Catch ex As Exception
    '        GenericHelper.ShowAlert(Me, ex.Message)
    '    End Try
    'End Sub

    'Protected Sub fvSrc_Updating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceCommandEventArgs) Handles fvSrc.Updating
    '    Try
    '        'ddlMenu
    '        Dim ddlMenu As DropDownList = fv.FindControl("ddlMenu")
    '        e.Command.Parameters("@menu_system_code").Value = ddlMenu.SelectedValue

    '        GenericHelper.GenerateScript(GenerateScriptLogType.Update, LogMenuType.stp_sub_menu, e.Command)
    '    Catch ex As Exception
    '        GenericHelper.ShowAlert(Me, ex.Message)
    '    End Try
    'End Sub

    Protected Sub fv_ItemInserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertedEventArgs) Handles FV.ItemInserted
        Try
            e.ExceptionHandled = True
            If e.Exception Is Nothing Then
                ' log insert
                GenericHelper.GenerateScript(GenerateScriptLogType.Insert, LogMenuType.stp_sub_menu, e.Values)
                GenericHelper.ShowAlertSuccessful(Me)
            Else
                Throw New Exception(e.Exception.Message)
            End If
        Catch ex As Exception
            e.KeepInInsertMode = True
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try
    End Sub

    Protected Sub fv_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertEventArgs) Handles fv.ItemInserting
        Try
            ' Server validation ---------------------------
            If e.Values("sub_menu_name").ToString().Trim() = "" Then
                Throw New Exception("กรุณากรอกชือเมนูย่อยระบบ")
            End If
            If e.Values("sub_menu_url").ToString().Trim() = "" Then
                Throw New Exception("กรุณากรอก URL ของเมนูย่อยระบบ")
            End If
            ' Server validation ---------------------------

            Dim ddlMenu As DropDownList = fv.FindControl("ddlMenu")
            e.Values("menu_system_code") = ddlMenu.SelectedValue
        Catch ex As Exception
            e.Cancel = True
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try
    End Sub
End Class
